/**
 * @description 获取localStorage
 * @param {String} key Storage名称
 * @return string
 */
export function localGet(key: string) {
  const value = window.localStorage.getItem(key)
  try {
    return window.localStorage.getItem(key) as string
  } catch (error) {
    console.log(error)
    return value
  }
}

/**
 * @description 存储localStorage
 * @param {String} key Storage名称
 * @param {Any} value Storage值
 * @return void
 */
export function localSet(key: string, value: any) {
  window.localStorage.setItem(key, value)
}

/**
 * @description 清除localStorage
 * @param {String} key Storage名称
 * @return void
 */
export function localRemove(key: string) {
  window.localStorage.removeItem(key)
}

/**
 * @description 清除所有localStorage
 * @return void
 */
export function localClear() {
  window.localStorage.clear()
}

/**
 * @description 动态设置标题
 */
export const setTitle = (title: string) => {
  if (!title) return

  document.title = title || '宏信达商贸平台'
  document
    .querySelector('meta[property="og:title"]')
    ?.setAttribute('content', title)
}

/**
 * @description 处理后端时间函数
 */
export const formatDateTime = (Time: string) => {
  const timestamp = new Date(Time).getTime()
  const date = new Date(timestamp)
  const year = date.getFullYear() // 获取年份
  const month = String(date.getMonth() + 1).padStart(2, '0') // 获取月份，月份从0开始，所以加1
  const day = String(date.getDate()).padStart(2, '0') // 获取日期
  const hours = String(date.getHours()).padStart(2, '0') // 获取小时
  const minutes = String(date.getMinutes()).padStart(2, '0') // 获取分钟
  const seconds = String(date.getSeconds()).padStart(2, '0') // 获取秒数

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
